package com.langsheng.lsintell.ui.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.langsheng.lsintell.R;
import com.langsheng.lsintell.ar.TestARActivity;
import com.langsheng.lsintell.cmd.request.LSRequest;
import com.langsheng.lsintell.config.LSLoginInfos;
import com.langsheng.lsintell.data.LSGenVipPwdReq;
import com.langsheng.lsintell.data.LSGenVipPwdRes;
import com.langsheng.lsintell.interfaces.LSResDataListener;
import com.langsheng.lsintell.network.LSNet;
import com.langsheng.lsintell.ui.widget.LSTimeCountDownView;
import com.langsheng.lsintell.utils.LSUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LSVipPwdFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LSVipPwdFragment extends LSBaseFragment {
    @BindView(R.id.et_lock_pwd)
    EditText etLockPwd;
    @BindView(R.id.et_serial)
    EditText etSerial;
    @BindView(R.id.view_count_down)
    LSTimeCountDownView viewCountDown;
    @BindView(R.id.btn_gen_temp_pwd)
    Button btnGenTempPwd;
    @BindView(R.id.ll_pwd_container)
    LinearLayout llPwdContainer;
    @BindView(R.id.tv_ad_image)
    ImageView tvAdImage;

    public LSVipPwdFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment LSVipPwdFragment.
     */
    public static LSVipPwdFragment newInstance() {
        LSVipPwdFragment fragment = new LSVipPwdFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lsvip_pwd, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        createVipPwdView("000000");
        btnGenTempPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptGen();
            }
        });
        tvAdImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), TestARActivity.class);
                startActivity(i);
            }
        });
    }

    private void createVipPwdView(String pwd) {
        for (char c : pwd.toCharArray()) {
            TextView view = createACharView();
            view.setText(String.valueOf(c));
            llPwdContainer.addView(view);
        }
    }

    private void attemptGen() {
        String lockPwd = etLockPwd.getText().toString();
        String serial = etSerial.getText().toString();
        if (TextUtils.isEmpty(lockPwd)) {
            etLockPwd.setError("指纹锁密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(serial)) {
            etSerial.setError("指纹锁序列号不能为空");
            return;
        }
        doGenPwd(lockPwd, serial);
    }

    private void doGenPwd(final String lockPwd, final String serial) {
        LSRequest request = new LSRequest() {
            @Override
            public void createCmd() {
                setCmdCode(0x2010);
                LSGenVipPwdReq req = new LSGenVipPwdReq();
                req.setKeypwd(lockPwd);
                req.setLockkey(serial);
                req.setToken(LSLoginInfos.getOurInstance().getLoginData().getToken());
                req.setTs(System.currentTimeMillis() + "");
                setContent(JSONObject.toJSON(req).toString());
            }
        };

        LSNet.getInstance().sendRequest(request, true, new LSResDataListener() {
            @Override
            public boolean onResponse(String content) {
                LSGenVipPwdRes vipPwd = JSONObject.parseObject(content, LSGenVipPwdRes.class);
                String pwd = vipPwd.getData().getVipkey();
                createVipPwdView(pwd);
                viewCountDown.setProgressColor(0xFF2DD101);
                viewCountDown.startCountDownTime(new LSTimeCountDownView.OnProgressFinishListener() {
                    @Override
                    public void progressFinished() {
                        viewCountDown.setProgressColor(Color.RED);
                    }
                });
                return false;
            }

            @Override
            public boolean onResponseErr(String errCode, String content) {
                return false;
            }
        });
    }

    private TextView createACharView() {
        TextView textView = new TextView(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(LSUtil.dp2px(getActivity(), 4), 0, 0, 0);
        textView.setLayoutParams(params);
        textView.setTextSize(LSUtil.dp2px(getActivity(), 10));
        textView.getPaint().setFlags(TextPaint.UNDERLINE_TEXT_FLAG);
        return textView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
